<?php
namespace Yzadmin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 后台友情链接控制器
 */
class LinksController extends AdminBaseController {

    public function index(){
        check_login();
        $str ='1=1 ';$page_num = 0; $page_num = 10;
        $count=D('Links')->where($str)->count();
        $Page = new \Think\Page($count,25);
        $show = $Page->show();// 分页显示输出
        $list = D('Links')->where($str)->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $key=>$val){
            if ($val['enddate'] < time()){
              $list[$key]['status']='<font class="red">已过期</font>';
            }else{
               $list[$key]['status']='显示';
            }
        }
        $assign=array(
            'lists'=>$list,
            'show'=>$show
        );
        $this->assign($assign);
        $this->display();
    }

    public function add(){
        check_login();
        if (IS_POST){
          $data = I('post.data');
          $data['startdate'] = strtotime($data['startdate']);
          $data['enddate'] = strtotime($data['enddate']);
          $result = D('Links')->add($data);
          if(false !== $result){
            $this->success('数据操作成功！',U('index'));exit;
          }else{
            $this->error('服务器正忙，请稍后再试！');
          }
        }
        $info['startdate'] = time();
        $info['enddate'] = strtotime('+2 Year');
        $cateType = D('Linkstype')->select();
        $assign=array(
            'cateType'=>$cateType,
            'info'=>$info
        );
        $this->assign($assign);
        $this->display('edit');
    }



    public function edit(){
        check_login();
        if (IS_POST){
          $data = I('post.data');$id = intval(I('post.id'));
          if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
          $data['startdate'] = strtotime($data['startdate']);
          $data['enddate'] = strtotime($data['enddate']);
          $result =D('Links')->where(array('id'=>$id))->save($data);
          if(false !== $result){
            $this->success('数据操作成功！',U('index'));exit;
          }else{
            $this->error('服务器正忙，请稍后再试！');
          }
        }
        $id=I('get.id');
        if (empty($id)){$this->error('非法操作');}
        $cateType = D('Linkstype')->select();
        $info=D('Links')->find($id);
        $assign=array(
            'cateType'=>$cateType,
            'info'=>$info,
            'id'=>$id
        );
        $this->assign($assign);
        $this->display();
    }

    /*删除操作*/
    function del($id = 0){
      if(IS_POST){
        if(empty($id)){echo 0;exit;}
        $result = D('Links')->where('id='.$id)->delete();
        if($result){
          echo 1;exit;
        }else{
          echo 0;exit;
        }
      }
    }


}
